package com.demo.gateway.mapper.provider;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;

/**
 * @author: demo
 * @date: 2020年6月22日
 * @Document :
 */
public class ProviderUtil {
    /**
     * 转换字符串 
     * @param property
     * @return
     */
	public  static  String   sqlString(String property) {
		return  String.format( "'%s'", property) ;
	}  
	/**
	 * 转换前置like
	 * @param property
	 * @return
	 */
	public  static  String   sqlLikeBefore(String property) {
		return  String.format( "%%%s", property) ;
	}  
	/**
	 * 转换后置like
	 * @param property
	 * @return
	 */
	public  static  String   sqlLikeEnd(String property) {
		return  String.format( "%s%%", property) ;
	}  
	/**
	 * 转换like
	 * @param property
	 * @return
	 */
	public  static  String   sqlLike(String property) {
		return  String.format( "%%%s%%", property) ;
	}  
	/**
	 * 转换insert  values
	 * @param objs
	 * @return
	 */
	public  static  String  sqlValues(Object  ...objs)  {
		StringBuilder sb=new StringBuilder();
		for (Object obj : objs) {
			if (obj instanceof String) {
				sb.append(sqlString(obj.toString())).append(",");
			}else {
				sb.append(obj.toString()).append(",");
			}
		}
		return StringUtils.substringBeforeLast(sb.toString(), ",");
	}
	
	/**
	 * 合成update语句 key1=value1
	 * @param key
	 * @param value
	 * @return
	 */
	public  static  String  sqlUpdate(String key,Object value) {
		if (value instanceof String) {
			value=sqlString(value.toString());
		}
		return String.format("%s=%s",key,value);
	}
	/**
	 * 批量update语句 key1=value1,key2=value2
	 * @param updateMap
	 * @return
	 */
	public  static  String  sqlUpdates(Map<String, Object> updateMap) {
		StringBuilder sb=new StringBuilder();
		for (String  key : updateMap.keySet()) {
			sb.append(sqlUpdate(key, updateMap.get(key))+",");
		}
		return StringUtils.substringBeforeLast(sb.toString(), ",");
	}
	/**
	 *  包含特殊符号 不解析
	 * @param str
	 * @return
	 */
	public  static  String   sqlNoParse(String str) {
		return  "<![CDATA[ "+str+" ]]>";
	}
	
	/**
	 * 替换el字符
	 * @param str
	 * @return
	 */
	public  static  String   sqlReverseEl(String str) {
		return str.replace("#{", "_#_");
	}
	
	/**
	 * 转回el字符
	 * @param str
	 * @return
	 */
	public  static  String   sqlParseEl(String str) {
		return str.replace("_#_", "#{");
	}
}
